import { GameConfigData, GameData } from "../common/GameConfig";
import { GameUIs } from "./GameUI";
import { GameViews } from "./GameView";

const {ccclass, property} = cc._decorator;

@ccclass
export default class nodeItem extends cc.Component {
   
    @property(sp.Skeleton)
    spPeople:sp.Skeleton = null;
    @property(cc.Sprite)
    progress:cc.Sprite = null;
    @property(Number)
    ID = 0;//记录第几个位置

    _progNum = 0;//进度条每0.2秒的长度
    _fishType = 0;//这个位置调的鱼类型
    _time = 0;//所剩时间
    _peopleType = 0;//人物类型
    _isCheck = "null";
    _isUnlock = false;
    _status = 0;

    setItemData(data){
        if(!data)return;
        this._status = data.status;
        if(data.status === 0 || data.status === 1)
        {
            if(data.status === 1)this._isUnlock = true;
            else this._isUnlock = false;
            this.progress.node.parent.active = false;
            this.spPeople.skeletonData = null;
        }
        else if(data.status === 2)
        {
            this._isUnlock = true;
            if(data.sp)this.spPeople.skeletonData = data.sp;
            this._progNum = (this.progress.node.width / data.etime * 0.1) / this.progress.node.width;
            this._time = data.etime;
            this._peopleType = data.index
            this._fishType = data.type;
            this.progress.node.parent.active = true
            this.setProgress(data.etime);
            this.setPeopleSkeletonAnim("idle",true)
            this.setItemState();
        }
        
    }

    setProgress(time){
        this.scheduleOnce(()=>{
            this.progress.fillRange = this.progress.fillRange + this._progNum;
            this._time  -= 0.1;
            if(this.progress.fillRange >= 1 || this._time === 0){
                this._isCheck = "finish";
            }
            else
            {
               this.setProgress(this._time ) 
            }
        },0.1 / GameConfigData.fishSpeed)
    }

    resetItemData(){
        this._fishType = 0;
        this._time = 0;
        this._peopleType = 0;
        this._isCheck = "null";
        this.setItemState();
        this.spPeople.skeletonData = null;
        this.progress.node.parent.active = false;
        this.progress.fillRange = 0;
    }

    setPeopleSkeletonAnim(animName,isLoop){
        this.spPeople.setAnimation(1,animName,isLoop);
    }

    setItemState(){
        GameData.fishData[this.ID] = {status:this._status,etime:this._time,people:this._peopleType,fish_id: this._fishType }
    }

    onButtonItemCallback(){
        if(this._status != 0)
        {
            if(this._isCheck == "null")
            {
                let call = (data)=>{
                    if(!data || !data.state)return
                    this._isCheck = "ready";
                    let endPosi = this.node.parent.convertToWorldSpaceAR(this.node.position);
                    let direction = this.ID <=2?"top":"bottom";
                    GameUIs.setPeopleWalkAnimAndEndPosi(endPosi,this.setItemData.bind(this),direction, data.fishType);
                }
               
                GameViews.showView("pfSelecFish",{callback:call.bind(this)})
            }
            else if(this._isCheck == "finish")
            {
                this.setPeopleSkeletonAnim("diaoyu2",false);
                this.scheduleOnce(()=>{
                    this.resetItemData()
                },1.8)
            }
        }
        else
        {
            //播放视频解锁位置
            console.log("show video unlock");
            let call = (res)=>{
                if(!res)return
                this._status = 1;
                this.setItemState();
            }

            call(true)
        }
        
    }
}
